package com.baoku.wap.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.baoku.wap.entity.Commont;
import com.baoku.wap.entity.User;
import com.baoku.wap.util.DB;
import com.baoku.wap.util.Md5;

public class UserFactory {
	
	public boolean saveUser(User user){
		Connection conn = DB.getConnection();
		Statement st = DB.getStatement(conn);
		ResultSet rs = null;
		try {
			int i = 0;
			rs = st.executeQuery("select count(*) from user where id = '"+user.getId()+"'");
			if (rs.next()) {
				 i = rs.getInt(1);
			}
			if ( i == 1){
				return false;
			}
			String password = new Md5().EncoderByMd5(user.getPassword());
			String sql = "insert into user (id,password,email,time) values ('"+user.getId()+"','"+password+"','"+user.getEmail()+"',now())";
			st.executeUpdate(sql);
		} catch (SQLException e) {
			DB.close(conn);
			DB.close(st);
			DB.close(rs);
			e.printStackTrace();
			return false;
		} finally {
			DB.close(conn);
			DB.close(st);
			DB.close(rs);
		}
		return true;
	}
	
	public boolean login(String id,String password){
		Connection conn = DB.getConnection();
		Statement st = DB.getStatement(conn);
		ResultSet rs = null;
		try {
			rs = st.executeQuery("select * from user where id = '"+id+"'");
			String getpassword = null ;
			if (rs.next()) {
				getpassword = rs.getString("password");
			}
			if ( new Md5().checkpassword(password, getpassword) ){
				return true;
			}
		} catch (SQLException e) {
			DB.close(conn);
			DB.close(st);
			DB.close(rs);
			e.printStackTrace();
			return false;
		} finally {
			DB.close(conn);
			DB.close(st);
			DB.close(rs);
		}
		return false;
	}
	
	public boolean saveCom(Commont commont){
		Connection conn = DB.getConnection();
		Statement st = DB.getStatement(conn);
		ResultSet rs = null;
		try {
			
			String sql = "insert into commont (content,userid,knowledgeid,time,islook) values ('"+commont.getCom()+"','"+commont.getUserid()+"',"+commont.getKnowledgeid()+",now(),0)";
			st.executeUpdate(sql);
		} catch (SQLException e) {
			DB.close(conn);
			DB.close(st);
			DB.close(rs);
			e.printStackTrace();
			return false;
		} finally {
			DB.close(conn);
			DB.close(st);
			DB.close(rs);
		}
		return true;
	}
	
	public boolean saveFavorite(String userid,int knowledgeid){
		if ( knowledgeid == 0){
			return false;
		}
		Connection conn = DB.getConnection();
		Statement st = DB.getStatement(conn);
		ResultSet rs = null;
		try {
			int i = 0;
			rs = st.executeQuery("select count(*) from favorite where userid = '"+userid+"' and knowledges="+knowledgeid);
			if (rs.next()) {
				 i = rs.getInt(1);
			}
			if ( i == 1){
				return false;
			}
			String sql = "insert into favorite (userid,knowledges) values ('"+userid+"',"+knowledgeid+")";
			st.executeUpdate(sql);
		} catch (SQLException e) {
			DB.close(conn);
			DB.close(st);
			DB.close(rs);
			e.printStackTrace();
			return false;
		} finally {
			DB.close(conn);
			DB.close(st);
			DB.close(rs);
		}
		return true;
	}	
	
}
